package utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

import com.opensymphony.xwork2.ActionSupport;

public class DBUtils {
    private static String driver;
    private static String user;
    private static String url;
    private static String password;
    static{
    	try {
//			Properties prop = new Properties();
//			prop.load(DBUtils.class.getClassLoader().getResourceAsStream("db.properties"));
//			driver = prop.getProperty("driver");
//			user = prop.getProperty("user");
//			url = prop.getProperty("url");
//			password = prop.getProperty("password");
    		ActionSupport actionSupport = new ActionSupport();
    		driver = actionSupport.getText("db.driver");
			user = actionSupport.getText("db.user");
			url = actionSupport.getText("db.url");
			password = actionSupport.getText("db.password");
			Class.forName(driver);
		} catch (Exception e) {
			throw new RuntimeException("数据库驱动加载失败",e);
		}
    }
    public static Connection getConnection() throws SQLException{
    	  Connection con = DriverManager.getConnection(url, user, password);
		return con;
    }
    public static void closeConnection(Connection con){
    	 if(con!=null){
    		 try {
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
    	 }
    }
    public static void main(String[] args) throws SQLException {
			System.out.println(getConnection());
	}
    
}
